package com.easy.sk.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.SeckillOrder;
import com.easy.common.service.ISeckillOrderService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/seckill/order")
@CrossOrigin
public class SeckillOrderController {

    @Autowired
    ISeckillOrderService seckillOrderService;

    // 根据id获取秒杀订单信息
    @GetMapping("/id/{id}")
    public Result getSeckillOrderById(@PathVariable Integer id) {
        return Result.success(seckillOrderService.getSeckillOrderById(id));
    }

    // 根据用户id获取秒杀订单信息
    @GetMapping("/userid/{userId}")
    public Result getSeckillOrderByUserId(@PathVariable Integer userId) {
        return Result.success(seckillOrderService.getSeckillOrderByUserId(userId));
    }

    // 添加秒杀订单
    @PostMapping("/add")
    public Result addSeckillOrder(@RequestBody SeckillOrder seckillOrder) {
        int result = seckillOrderService.addSeckillOrder(seckillOrder);
        return result > 0 ? Result.success("添加成功") : Result.fail(500, "添加失败");
    }

    // 更新秒杀订单
    @PutMapping("/update")
    public Result updateSeckillOrder(@RequestBody SeckillOrder seckillOrder) {
        int result = seckillOrderService.updateSeckillOrder(seckillOrder);
        return result > 0 ? Result.success("更新成功") : Result.fail(500, "更新失败");
    }

    // 删除秒杀订单（软删除）
    @DeleteMapping("/delete/{id}")
    public Result deleteSeckillOrder(@PathVariable Integer id) {
        int result = seckillOrderService.deleteSeckillOrder(id);
        return result > 0 ? Result.success("删除成功") : Result.fail(500, "删除失败");
    }

    // 获取秒杀订单列表
    @GetMapping("/list")
    public Result getSeckillOrderList(SeckillOrder seckillOrder) {
        return Result.success(seckillOrderService.getAllSeckillOrder(seckillOrder));
    }

    // 获取秒杀订单分页列表
    @GetMapping("/page")
    public Result getSeckillOrderPage(SeckillOrder seckillOrder, Integer current, Integer size) {
        IPage<SeckillOrder> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        return Result.success(seckillOrderService.getSeckillOrderPage(seckillOrder, page));
    }
}
